package com.taotao.cart.controller.api;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.taotao.cart.pojo.Cart;
import com.taotao.cart.service.CartService;

@Controller
@RequestMapping("api/cart")
public class ApiCartController {

	@Autowired
	private CartService cartService;

	@GetMapping("{userId}")
	public ResponseEntity<List<Cart>> toCart(@PathVariable("userId")Long userId) {

		try {
			// 登陆状态的购物车
			List<Cart> list = this.cartService.queryCarts(userId);
			if(CollectionUtils.isEmpty(list)){
				return ResponseEntity.notFound().build();
			}
			return ResponseEntity.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

}
